package com.lyp.adapters;

import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lyp.jsonbean.NofenpeiBean;
import com.lyp.yangxixi.missionmanagerfragment.NoAllotMissionFragment;
import com.tjl.yangxixis.R;

import java.util.List;

/**
 * Created by Administrator on 2017/6/4.
 */

public class NofenpeiAdapter extends RecyclerView.Adapter<NofenpeiAdapter.MyViewHolder> {

    private NofenpeiAdapter.MyItemClickListener mListener;
    private List<NofenpeiBean.DataBean> mList;
    /**
     * PS:比 HashMap<Integer, Boolean> 性能更高 by lzh
     */
    private SparseBooleanArray mSparseBooleanArray = new SparseBooleanArray();

    public NofenpeiAdapter(List<NofenpeiBean.DataBean> list, SparseBooleanArray sparseBooleanArray) {
        mList = list;
        mSparseBooleanArray = sparseBooleanArray;
    }

    @Override
    public NofenpeiAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        NofenpeiAdapter.MyViewHolder holder = new NofenpeiAdapter.MyViewHolder(
                LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_undistributed, viewGroup,
                        false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder myViewHolder, int i) {
        final int position = myViewHolder.getAdapterPosition();

        myViewHolder.mName.setText(mList.get(position).getUser_name());
        myViewHolder.mTime.setText(mList.get(position).getPay_time());

        // 设置 CheckBox 选择状态 by lzh
        myViewHolder.mcheckBox.setChecked(mSparseBooleanArray.get(position, false));
        // 设置 CheckBox 改变事件监听  by lzh
        myViewHolder.mcheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSparseBooleanArray.put(position, isChecked);
                // 根据本次选择状态控制界面是展示全选按钮还是全不选按钮 by lzh
                if (isChecked) {
                    int count = 0;
                    for (int i = 0; i < mList.size(); i++) {
                        if (mSparseBooleanArray.get(i)) {
                            count++;
                        } else {
                            // 跳过本次循环,一旦有一个按钮为未选状态就说明按钮还是应该保持原状,剩下的循环也不用走了 by lzh
                            break;
                        }
                    }
                    if (count == mList.size()) {
                        NoAllotMissionFragment.mAllTask.setText("全不选");
                        NoAllotMissionFragment.mCheckbox.setChecked(true);
                    }
                } else {
                    NoAllotMissionFragment.mAllTask.setText("全选");
                    NoAllotMissionFragment.mCheckbox.setChecked(false);
                }
                // 打印 by lzh
                //LogUtils.i("test", "size：" + mSparseBooleanArray.size() + "; key:" + mSparseBooleanArray.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * 设置Item点击监听
     * @param listener
     */
    public void setOnItemClickListener(NofenpeiAdapter.MyItemClickListener listener){
        this.mListener = listener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        CheckBox mcheckBox;
        TextView mName;
        TextView mTime;
        LinearLayout mNofenpei;

        public MyViewHolder(View view) {
            super(view);
            mcheckBox = (CheckBox) view.findViewById(R.id.checkBox);
            mName = (TextView) view.findViewById(R.id.tv_notaskname);
            mTime = (TextView) view.findViewById(R.id.tv_notasktime);
            mNofenpei = (LinearLayout) view.findViewById(R.id.ll_nofenpei);
            mNofenpei.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(v,getPosition());
                }
            });
        }
    }

    /**
     * item点击接口
     */
    public interface MyItemClickListener {
        void onItemClick(View v, int position);
    }
}
